layui.use(['form', 'layer', 'table', 'viewer'], function () {

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;

    var Viewer = layui.viewer;

    var datalist_url = $('.datalist_url').val();
    var add_url = $('.add_url').val();
    var edit_url = $('.edit_url').val();
    var delete_url = $('.delete_url').val();
    var save_url = $('.save_url').val();


    if ($('#dataTable').length > 0) {
        // Table 列表
        var tableIns = table.render({
            elem: '#dataTable',
            url: datalist_url,
            page: true,
            cellMinWidth: 95,
            //height : "full-100",
            limit: 20,
            limits: [10, 20, 30, 50, 100],
            id: "tableList",
            initSort: {
                field: 'created_at', // 排序字段，对应 cols 设定的各字段名
                type: 'desc' // 排序方式  asc: 升序、desc: 降序、null: 默认排序
            },
            cols: [[
                {field: 'id', title: '序号', minWidth: 120, align: "center", sort: true},
                {
                    field: 'full_avatar', title: '用户头像', align: "center", templet: function (data) {
                        return '<a class="layer-photos" href="javascript:;"><img src="' + data.full_avatar + '" layer-src="' + data.full_avatar + '" style="height: 100%;" /></a>';
                    }
                },
                {field: 'name', title: '用户名', minWidth: 180, align: "center"},
                {field: 'mobile', title: '手机号', minWidth: 120, align: "center"},
                {field: 'cash', title: '用户余额', align: "center", sort: true},
                {field: 'created_at', title: '创建时间', minWidth: 180, align: "center", sort: true},
                {
                    title: '操作', fixed: "right", align: "center", minWidth: 120, templet: function (data) {
                        var html = '';
                        html += '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>';

                        return html;
                    }
                }
            ]],
            done: function (res, curr, count) {
                // 相册层
                // layer.photos({
                //     photos: '.layer-photos',
                //     anim: 5, //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                // });

                var table_body = document.getElementsByClassName('layui-table-body');
                var galley = table_body[0].getElementsByClassName('layui-table');
                //var galley = galley[0].getElementsByClassName('layer-photos');
                COMMON.viewImage(Viewer, galley);

                //$('thead tr').find('th').eq(4).css({'color': '#FF5722'});
            }
        });

        // 搜索
        $(".search_btn").on("click", function () {
            var name = $('.searchBox').find('input[name=name]').val();
            var mobile = $('.searchBox').find('input[name=mobile]').val();

            if (name != '' || mobile != '') {
                table.reload("tableList", {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        name: name,
                        mobile: mobile,
                    }
                });
            } else {
                layer.msg("请选择筛选条件");
            }
        });

        // 清空搜索
        $(".reset_btn").on("click", function () {
            table.reload("tableList", {
                where: {
                    name: '',
                    mobile: '',
                }
            });
        });

        $('.add_btn').on('click', function () {
            formHtml();
        });

        // 列表操作
        table.on('tool(dataTable)', function (obj) {
            var layEvent = obj.event,
                data = obj.data;
            // 删除
            if (layEvent === 'del') {
                layer.confirm('确定删除此数据？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        url: delete_url,
                        data: {"id": data.id},
                        type: "POST",
                        dataType: "json",
                        success: function (res) {
                            if (res.status == 1) {
                                layer.msg('已删除', {icon: 1, time: 1000});
                                tableIns.reload();
                                layer.close(index);
                            } else {
                                layer.msg(res.msg);
                            }
                        },
                        error: function (data) {
                            layer.msg("服务器无响应");
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                formHtml(data);
            }
        });

        // Form 表单
        function formHtml(data) {
            var content = add_url;

            if (data) {
                content = edit_url + '?id=' + data.id;
            }

            var index = layer.open({
                title: "会员管理",
                type: 2,
                area: ["600px", "500px"],
                content: content,
                success: function (layero, index) {
                    form.render();
                }
            });
        }
    }

    // 自定义验证规则
    form.verify({
        name: function (value) {
            if (value == '') {
                return '姓名不能为空';
            }
        },
    });

    // 表单提交
    form.on("submit(laySave)", function (data) {
        submitForm(data);
    });

    // 表单提交
    function submitForm(data) {
        // 弹出loading
        var index = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        $.ajax({
            url: save_url,
            data: data.field,
            type: "post",
            dataType: "json",
            success: function (res) {
                top.layer.close(index);
                top.layer.msg(res.msg);
                if (res.status == 1) {
                    layer.closeAll("iframe");
                    //parent.window.tableIns.reload();
                    parent.layui.table.reload('tableList', {});
                }
            },
            error: function (data) {
                layer.msg("服务器无响应");
            }
        });
    }
});